/**
 * Author:  zhzhy.
 * Date:    2017/9/22.
 * File：   vue入口文件
 */
/* eslint-disable */
// require('../assets/less/common.less')
import Vue from 'vue'
import 'babel-polyfill'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import CryptoJS from 'crypto-js'
import * as utils from '../utils'
import router from '../router/index'
import request from '../request/index'
import * as filters from '../filter'
import registComponents from '../regist-components'

require('../assets/less/common.less')

Vue.config.productionTip = false
Vue.use(ElementUI)
registComponents(Vue)
Vue.use(CryptoJS)
Vue.prototype.$request = request
Vue.prototype.$utils = utils
// 实例化Vue的filter
Object.keys(filters).forEach(k => Vue.filter(k, filters[k]))

/**
 * 存储初始化所需数据到vuex中
 * @returns {Promise.<void>}
 */
async function getFromLocalStorage() {
  await setParams()
  window.localStorage.getItem('userId')
}

/**
 * 从url上获取相应name参数值
 * @param name
 * @returns {*}
 */
function getUrlParam(name) {
  let tmpSearch = window.location.href.split('?')[1]
  console.log('name', name, tmpSearch)
  if (tmpSearch) {
    console.log('****')
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
    let r = tmpSearch.match(reg)
    if (r != null) return decodeURI(r[2])
    return null
  }
}

/**
 * 从url或者localStorage上获取数据存储到vuex中
 * @returns {Promise.<void>}
 */
async function setParams() {
  let userIdTmp = getUrlParam('userId') || 'U5BCE0E1073424B488DA9E7F43D0E6098'
  let pointId = getUrlParam('pointId') || ''
  window.localStorage.setItem('pointId', pointId)
  window.localStorage.setItem('userId', userIdTmp)
  await getConfigJson()
}

function getConfigJson() {

  // return axios.get('../static/serverconfig.json').then((result) => {
  //   console.log('read from json', result)
  //   window.localStorage.setItem('serverHost', result.data.serverHost)
  //   window.localStorage.setItem('serverPort', result.data.serverPort)
  //   window.localStorage.setItem('clientId', result.data.clientId)
  //   window.websocket = new WebSocket(result.data.websocketUrl)
  //   window.websocket.onopen = (e) => {
  //     console.log(e)
  //   }
  //   window.websocket.onerror = (e) => {
  //     console.log(e)
  //   }
  //   window.websocket.onmessage = (e) => {
  //     console.log(e)
  //   }
  //   window.websocket.onclose = (e) => {
  //     console.log(e)
  //   }
  // }).catch(() => {
  //   window.localStorage.setItem('serverHost', 'http://218.106.157.163')
  //   window.localStorage.setItem('serverPort', '7700')
  //   window.localStorage.setItem('clientId', '2E7966B47EA04112BBC03F3B9992E6F7')
  //   window.websocket = new WebSocket('ws://172.30.0.213:9090')
  //   window.websocket.onopen = (e) => {
  //     console.log(e)
  //   }
  //   window.websocket.onerror = (e) => {
  //     console.log(e)
  //   }
  //   window.websocket.onmessage = (e) => {
  //     console.log(e)
  //   }
  //   window.websocket.onclose = (e) => {
  //     console.log(e)
  //   }
  // })
}

/**
 * 初始化所需参数然后启动vue
 */
// getFromLocalStorage().then(() => {
// })
/* eslint-disable */
let pointId = getUrlParam('pointId') || ''
window.localStorage.setItem('pointId', pointId)
let serverConfig = document.createElement('script')
serverConfig.src= 'static/serverconfig.json?callback=readServerConfig'
document.body.appendChild(serverConfig)
serverConfig.onload =() => {
  new Vue({
    router
  }).$mount('#app')
}

